function formatNumber (n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

export function formatTime (date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}

function userOpenId(){
  return new Promise((resolve,reject)=>{
    wx.login({
      success:res=>{
        let {code} = res;
        wx.request({
          url:`https://api.weixin.qq.com/sns/jscode2session?appid=wxf94f96c7558d3b39&secret=9ce706c023e6f518df31c3a3b2dd5c46&js_code=${code}&grant_type=authorization_code`,
          success:results=>{
            resolve(results.data.openid)
          },
          error:err=>{
            reject(err)
          }
        })
      }
    })
  })
}

export default {
  formatNumber,
  formatTime,
  userOpenId
}
